package com.algorithm;

/**
 * 反转链表
 */
public class ReverseLinkedListLi {

    public static ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode prev=null;
        ListNode curr=head;
        ListNode next;
        for (int i=1;i<left;i++){
            prev=curr;
            curr=curr.next;
        }
        ListNode prev2=prev;
        ListNode curr2=curr;

        for (int i=left;i<=right;i++){
            next=curr.next;
            curr.next=prev;
            prev=curr;
            curr=next;
        }
        if (prev2!=null){
            prev2.next=prev;
        }else {
            head=prev;
        }
        curr2.next=curr;
        return head;
    }

    public static void main(String[] args) {
        ListNode listNode=new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5)))));
        ListNode listNode1 = reverseBetween(listNode, 2, 4);
        System.out.println(listNode1);
    }

}
